package com.ljh.action;

import com.ljh.pojo.Books;
import com.ljh.pojo.UserBook;
import com.ljh.service.BookService;
import com.ljh.service.BookUserService;
import com.opensymphony.xwork2.ActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("userbook")
public class UserBookAction {
    private String id;
    private int bookid;
    private String bookname;
    private int bookcount;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public int getBookid() {
        return bookid;
    }

    public void setBookid(int bookid) {
        this.bookid = bookid;
    }

    public String getBookname() {
        return bookname;
    }

    public void setBookname(String bookname) {
        this.bookname = bookname;
    }

    public int getBookcount() {
        return bookcount;
    }

    public void setBookcount(int bookcount) {
        this.bookcount = bookcount;
    }

    @Autowired
    @Qualifier("BookUserServiceImpl")
    private BookUserService bookUserService;
    @Autowired
    @Qualifier("BookServiceImpl")
    private BookService bookService;
    public String userallbook(){
        List<UserBook> userBooks=bookUserService.queryAllUserBook();
        ActionContext.getContext().getValueStack().set("userbooklist",userBooks);
        return "admin/allBookUser";
    }
    public String del(){
        UserBook userBook=new UserBook(this.id,this.bookid,null,this.bookcount);
        bookService.updateBookCount(this.bookid,(this.bookcount)*(-1));
        bookUserService.deleteUserBook(userBook);
        return "userbook/allBookUser";
    }
    public String user(){
        List<Books> list=bookService.queryAllBook();
        ActionContext.getContext().getValueStack().set("list",list);
        return "user/user";
    }
    public String addUserBook(){
//        HttpSession session=httpServletRequest.getSession();
        UserBook userBook=new UserBook(ActionContext.getContext().getSession().get("user").toString(),this.bookid,this.bookname,1);
        if(bookUserService.queryAllUserBookByIdByName(userBook).size()>0){
            bookUserService.updateUserBook(userBook);
            bookService.updateBookCount(this.bookid,1);
        }else{
            bookUserService.addUserBook(userBook);
            bookService.updateBookCount(this.bookid,1);
        }
        return "userbook/user";
    }
    public String userborrow(){
        List<UserBook> list=bookUserService.queryAllUserBookById(ActionContext.getContext().getSession().get("user").toString());
        System.out.println(list);
        ActionContext.getContext().getValueStack().set("userborrow",list);
        return "user/userborrow";
    }
}
